#include "ringqueue.h"

RingQueue::RingQueue(int q_size):
    m_RingQ(q_size)
{
}

bool RingQueue::push(FreyaData value)
{
    if(isFull())
    {
        return false;
    }
    else
    {
        m_RingQ.queue[m_RingQ.tail] = value;
        m_RingQ.tail = (m_RingQ.tail + 1) % m_RingQ.size;
    }

    if(m_RingQ.head == m_RingQ.tail)
    {
        m_RingQ.flag = F_FULL;
    }
    return true;
}

bool RingQueue::pull(FreyaData &value)
{
    if(isEmpty())
    {
        return false;
    }
    value = m_RingQ.queue[m_RingQ.head];
    m_RingQ.head = (m_RingQ.head + 1) % m_RingQ.size;

    if(m_RingQ.head == m_RingQ.tail)
    {
        m_RingQ.flag = F_EMPTY;
    }
    return true;
}

int RingQueue::size()
{
    if(isFull())
    {
        return m_RingQ.size;
    }
    else
    {
        return ((m_RingQ.tail + m_RingQ.size - m_RingQ.head) % m_RingQ.size);
    }
}

bool RingQueue::isEmpty()
{
    return ((m_RingQ.head == m_RingQ.tail) && F_EMPTY == m_RingQ.flag);
}

bool RingQueue::isFull()
{
    return ((m_RingQ.head == m_RingQ.tail) && F_FULL == m_RingQ.flag);
}
